<?php

namespace app\api\controller;

use app\admin\logic\user\VipLogic;
use app\api\logic\UserVipCardLogic;
use app\basic\ApiController;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("会员卡接口")
 */
class VipController extends ApiController
{
    // 无需登录的接口
    protected $noNeedLogin = ['getVipList','getVipRights'];

    protected $vip_logic;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new VipLogic();
        $this->vip_logic = new UserVipCardLogic();
        parent::__construct();
    }

   /**
     * @Apidoc\Title("获取会员信息")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="object", desc="会员信息", children={
     *   @Apidoc\Returned("is_vip", type="int", desc="是否是会员：0=否 1=是"),
     *   @Apidoc\Returned("vip_title", type="string", desc="会员卡名称"),
     *   @Apidoc\Returned("expire_time", type="string", desc="到期时间"),
     *   @Apidoc\Returned("status", type="int", desc="状态：0=已过期 1=使用中")
     * })
     */
    public function getVipInfo()
    {
        if (!$this->auth->isLogin()) {
            return $this->fail('请先登录');
        }
        $result = $this->vip_logic->getVipInfo($this->auth->getUserId());
        if ($result === false) {
            return $this->fail($this->vip_logic->getError());
        }
        return $this->success($result);
    }

    /**
     * @Apidoc\Title("获取会员卡列表")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="array", desc="会员卡列表", children={
     *     @Apidoc\Returned("id", type="int", desc="会员卡ID"),
     *     @Apidoc\Returned("name", type="string", desc="会员卡标题"),
     *     @Apidoc\Returned("adv", type="string", desc="角标文字"),
     *     @Apidoc\Returned("price", type="float", desc="售卖价格"),
     *     @Apidoc\Returned("sprice", type="float", desc="原价"),
     *     @Apidoc\Returned("discount", type="float", desc="折扣"),
     *     @Apidoc\Returned("day", type="int", desc="天数")
     * })
     */
    public function getVipList()
    {
        $list = $this->logic->getVipList();
        return $this->success($list);
    }

    /**
     * @Apidoc\Title("获取会员卡权益")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("vip_id", type="int", require=true, desc="会员卡ID")
     * @Apidoc\Returned("data", type="object", desc="会员卡权益", children={
     *     @Apidoc\Returned("vip_name", type="string", desc="会员卡名称"),
     *     @Apidoc\Returned("download", type="object", desc="下载权益", children={
     *         @Apidoc\Returned("name", type="string", desc="权益名称"),
     *         @Apidoc\Returned("value", type="int", desc="下载次数"),
     *         @Apidoc\Returned("unit", type="string", desc="单位")
     *     }),
     *     @Apidoc\Returned("discount", type="object", desc="折扣权益", children={
     *         @Apidoc\Returned("name", type="string", desc="权益名称"),
     *         @Apidoc\Returned("value", type="float", desc="折扣值"),
     *         @Apidoc\Returned("unit", type="string", desc="单位")
     *     }),
     *     @Apidoc\Returned("duration", type="object", desc="有效期", children={
     *         @Apidoc\Returned("name", type="string", desc="权益名称"),
     *         @Apidoc\Returned("value", type="int", desc="天数"),
     *         @Apidoc\Returned("unit", type="string", desc="单位")
     *     })
     * })
     */
    public function getVipRights()
    {
        $vipId = $this->request->post('vip_id', 0);
        if (empty($vipId)) {
            return $this->fail('会员卡ID不能为空');
        }
        
        $rights = $this->logic->getVipRights($vipId);
        return $this->success($rights);
    }
}